package com.sugon.api;

import cn.hutool.core.map.MapUtil;
import com.sugon.annotation.AuthCheck;
import com.sugon.annotation.LoginUser;
import com.sugon.dao.ErmsFileDao;
import com.sugon.entity.AnnexFileEntity;
import com.sugon.entity.ApiResult;
import com.sugon.entity.SysUserEntity;
import com.sugon.entity.dto.ERMSFileStatDTO;
import com.sugon.service.AnnexFileService;
import com.sugon.util.ApiBaseAction;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "ERMS档案API，包括设备、模具、工装、备件中档案列表和附件")
@RestController
@RequestMapping("/api/vi/files")
public class ErmsFilesApiController extends ApiBaseAction {

    @Autowired
    private ErmsFileDao ermsFileDao;
    @Autowired
    private AnnexFileService annexFileService;

    /**
     * 模块档案类别查询API
     * @param loginUser
     * @param params
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "模块档案类别查询API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "moduleId", value = "模块ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "moduleType", value = "模块类别", paramType = "query", dataType = "String", required = true, allowableValues = "1,2,3,4,5", example = "1设备类 2模具类3备件类4工装类5其他类"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "int", required = true)
    })
    @PostMapping
    public ApiResult<PageUtils> loadMoldFiles(@ApiIgnore @LoginUser SysUserEntity loginUser, @ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<PageUtils> apiResult = new ApiResult<>();
        Query query = new Query(params);
        String moduleId = MapUtil.getStr(params, "moduleId");
        Integer moduleType = MapUtil.getInt(params, "moduleType");
        List<ERMSFileStatDTO> list = ermsFileDao.statModuleFile(moduleId, moduleType);
        return apiResult.success(new PageUtils(list, list.size(), query.getLimit(), query.getPage()));
    }

    /**
     * 档案附件查询API
     * @param loginUser
     * @param params
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "档案附件查询API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileTypeId", value = "档案类别ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "moduleType", value = "模块类别", paramType = "query", dataType = "String", required = true, allowableValues = "1,2,3,4,5", example = "1设备类 2模具类3备件类4工装类5其他类"),
            @ApiImplicitParam(name = "moduleId", value = "模块类别", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "int", required = true)
    })
    @PostMapping("/equList")
    public ApiResult<PageUtils> equList(@ApiIgnore @LoginUser SysUserEntity loginUser, @ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<PageUtils> apiResult = new ApiResult<>();
        //查询列表数据
        Query query = new Query(params);
        List<AnnexFileEntity> annexFileList = annexFileService.queryEquList(query);
        int total = annexFileService.queryEquTotal(query);
        return apiResult.success(new PageUtils(annexFileList, total, query.getLimit(), query.getPage()));
    }

}
